package threading.tests;

import org.junit.*;

public class ReceiveCoordinatorTests extends TestUnitBase {

	@Test
	public void enqueue() throws Exception {
					
		newLatch(2);
		_coord.receive(_trace, true);
		
		_trace.enqueue("enqueue-1");
		_trace.enqueue("enqueue-2");
		
		awaitLatch();
	}
	
	@Test
	public void enqueueVolatileAndReactivate() throws Exception {
	
		newLatch(1);
		// TODO: fix bug in  ReceiveCoordinator, this test will block
		// forever since the enqueued arguments never are posted to execute()
		_coord.receive(_trace, false);
		
		_trace.enqueue("enqueueVolatile-1");
		_trace.enqueue("enqueueVolatile-2");		
		
		awaitLatch();
		int queueCount = _queue.count();
		int traceCount = _trace.count();
		
		System.out.println( traceCount );		
		assertTrue( traceCount == 1 ); // single argument enqueued at port
		
		// reactivate a receiver on the port
		newLatch(1);
		_coord.receive(_trace, false);
		awaitLatch();
		assertTrue( _queue.count() == 0 ); // no tasks should be enqueued for execution
		assertTrue( _trace.count() == 0 ); // single argument enqueued at port
	}
}
